package com.auth.main.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * @auther zhangwj
 * @date 2021/3/8 下午6:17
 */
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Bean
    @Override
    protected AuthenticationManager authenticationManager() throws Exception {
        return super.authenticationManager();
    }

    @Bean
    @Override
    protected UserDetailsService userDetailsService() {
        return super.userDetailsService();
    }

    public static void main(String[] args) {
        String pwd = new BCryptPasswordEncoder().encode("123456");
        System.out.println(pwd);
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
                .withUser("admin").password("$2a$10$Q5EMZUHznhpBFW2xheYy3OVIKSrHTGbzjNu1argyQkOF5NdgSHI6O").roles("admin")
                .and()
                .withUser("user").password("$2a$10$Q5EMZUHznhpBFW2xheYy3OVIKSrHTGbzjNu1argyQkOF5NdgSHI6O").roles("user");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.antMatcher("oauth/**")
                .authorizeRequests().antMatchers("oauth/**").permitAll()
                .and().csrf().disable();
    }
}
